export const useWindowUnload = createGlobalState(() => {
  const isInterdict = ref(false)

  function interdict() {
    window.onbeforeunload = (event) => {
      event.preventDefault()
      event.returnValue = ''
    }
    isInterdict.value = true
  }

  function clearInterdict() {
    window.onbeforeunload = null
    isInterdict.value = false
  }

  watch(
    () => isInterdict.value,
    (e) => {
      if (e) {
        interdict()
      } else {
        clearInterdict()
      }
    },
  )

  return { isInterdict, interdict, clearInterdict }
})
